import { apiUrl } from './api.js'

const request = (_URL, _TYPE, _DATA, _CALLBACK) => {
    let _SESSIONID = wx.getStorageSync('_SESSIONID_')
    let _HEADER = { 'content-type': 'application/json' }
    _SESSIONID ? Object.assign(_HEADER, { 'Cookie': 'PHPSESSID=' + _SESSIONID }) : ''
    wx.request({
        url: apiUrl + _URL,
        method: _TYPE,
        data: _DATA,
        header: _HEADER,
        success: ((res) => {
            const data = res.data
            if (data.code === 1) {
                _CALLBACK && _CALLBACK(data);
            }
            if (data.code === 0) {
                wx.showToast({
                    icon: 'none',
                    title: data.msg,
                    duration: 2000
                })
            }
        }),
        fail: ((res) => {
            console.log(res)
        })
    })
}

module.exports = {
    request: request
}